const {
    Op
} = require('sequelize')

const {
    flatten
} = require('lodash')

const {
    Movie,
    Sentence,
    Music
} = require('./classic')



class Art{
    constructor(art_id, type) {
        this.art_id = art_id
        this.type = type
    }

    static async getList(artInfoList) {
       const artInfoObj = {
           100: [],
           200: [],
           300: []
       }
       for(let artInfo of artInfoList) {
        artInfoObj[artInfo.type].push(artInfo.art_id);
       }
       let arts = [];
       for(let key in artInfoObj) {
        const ids = artInfoObj[key];
        if(ids.length === 0) {
           continue;
        }
        key = parseInt(key)
        arts.push(await Art._getListByType(ids, key)); 
       }
       return flatten(arts);
    }

    static async _getListByType(ids, type) {
       const finder = {
           where: {
               id: {
                   [Op.in]: ids
               }
           }
       }

       let art = null;
        switch (type) {
            case 100:
                art = await Movie.findAll(finder)
                break;
            case 200:
                art = await Music.findAll(finder)
                break;
            case 300:
                art = await Sentence.findAll(finder)
                break;
            case 400:
    
                break;       
            default:
                break;
        }
        return art;
    }

    static async getData(art_id, type) {
        const finder = {
           where: {
            id:art_id 
           }
        };
        let art = null;
        switch (type) {
            case 100:
                art = await Movie.findOne(finder)
                break;
            case 200:
                art = await Music.findOne(finder)
                break;
            case 300:
                art = await Sentence.findOne(finder)
                break;
            case 400:
                break;       
            default:
                break;
        }
        return art;
    }
}

module.exports = {
    Art
}